//
//  AppDelegate.m
//  便签本
//
//  Created by  on 2022/10/4.
//

#import "AppDelegate.h"
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self setupLocalUser];
    BaseTabBarViewController *vc = [[BaseTabBarViewController alloc] init];
    vc.tabBar.hidden = YES;
    CGRect frame = [[UIScreen mainScreen] bounds];
    self.window = [[UIWindow alloc] initWithFrame:frame];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)setUser:(User *)user{
    _user = user;
    if ([_user.account isEqualToString:@""]) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:accountkey];
    }else{
        [[NSUserDefaults standardUserDefaults] setValue:_user.account forKey:accountkey];
    }
    [self setupDefaultSign];
    [[NSNotificationCenter defaultCenter] postNotificationName:notifyUserUpdate object:nil];
}

- (void)setupLocalUser{
    bg_setDisableCloseDB(YES);
    NSString *account = [[NSUserDefaults standardUserDefaults] valueForKey:accountkey];
    if (account != nil && account.length > 0 && [account isEqualToString:@"去登录"] == NO) {
        [UserTool find:account complete:^(User * _Nonnull user) {
            self.user = user;
        }];
    }else{
        [UserTool register:@"O1234" psw:@"1234" complete:^(NSDictionary * _Nonnull dict) {}];
        self.user = [User new];
    }
}
///添加默认标签
- (void)setupDefaultSign{
    NSString * where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(APP.user.account)];
    NSInteger count = [SignModel bg_count:nil where:where];
    if (count <= 6) {
        NSArray *titles = @[@"工作",@"心情",@"生活"];
        NSArray *imgs = @[@"card-1",@"card-2",@"card-3"];
        NSMutableArray *signs = [NSMutableArray array];
        for (int i = 0; i < titles.count; i ++) {
            SignModel *sign = [SignModel new];
            sign.title = LS(titles[i]);
            sign.account = self.user.account;
            sign.ID = [NSString stringWithFormat:@"%d",i+1];
            UIImage *img = [UIImage imageNamed:imgs[i]];
            sign.cover = UIImageJPEGRepresentation(img, 0.5);
            [signs addObject:sign];
        }
        [SignModel bg_saveOrUpdateArrayAsync:signs complete:^(BOOL isSuccess) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:notifySignUpdate object:nil];
                
            });
        }];
    }
}
- (UIWindow *)meWindow{
    if (!_meWindow) {
        _meWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _meWindow.windowLevel = UIWindowLevelAlert + 1;
        _meWindow.rootViewController = [[MineViewController alloc] init];
        [_meWindow makeKeyAndVisible];
        _meWindow.hidden = YES;
    }
    return _meWindow;
}
- (NSString *)mainColor{
    if (!_mainColor) {
        NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:localMaincolorKey];
        _mainColor = value == nil || value.length <= 0 ? @"#F2F9F8":value;
    }
    return _mainColor;
}
@end
